import { defineStore } from "pinia";
import { getCartList } from '@/api/index'
let cartStore = defineStore('cartStore', {
    state: () => {
        return {
            cartList: [], //购物车列表数据
        }
    },
    actions: {
        async getCartListAsync() {
            let data = await getCartList();
            console.log(data);
            this.changeCartList(data.data || []);
            return data
        },
        // 给每个购物车都添加checked属性
        changeCartList(list) {
            this.cartList = list.map(item => {
                item.checked = true;
                return item;
            })
        }
    },
    getters: {
        cartNum() {
            return this.cartList.reduce((prev, next) => {
                return prev + next.goodsCount
            }, 0)
        }
    }
})
export default cartStore